function tournament_finish(id){
	jQuery.post(BASE_URI+'admin/dataman/tournament/tournament_finish',{ tournament_id:id },function(res){
		if(res== 'ok'){
			reload();
		}else{
			alert(res);
		}
	});
}

function tournament_cancel(id){
	if(confirm("Are you sure want to cancel this tournament?")){
		jQuery.post(BASE_URI+'admin/dataman/tournament/tournament_cancel',{ tournament_id:id },function(res){
			if(res== 'ok'){
				reload();
			}else{
				alert(res);
			}
		});
	}
}

function rank_view(id){
	jQuery.post(BASE_URI+'admin/dataman/tournament/rank_view',{tournament_id:id},function(res){
		jQuery('#dialog').html(res);
		jQuery('#dialog').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:400,
				 title: 'Rank view in tournament' 
			}
		);
	});
}

function tournament_publish_unpublish(id){
	jQuery.post(BASE_URI+'admin/dataman/tournament/publish_unpublish',{id:id},function(res){
		if(res == 'ok'){
			reload();
		}else{
			alert(res);
		}
	});
}

function prize_delete(p_id){
	if(confirm("Are you sure want to delete this item?")){
		jQuery.post(BASE_URI+'admin/dataman/tournament/delete_prize',{id:p_id},function(res){
			if(res == 'ok'){
				reload();
			}else{
				alert(res);
			}
		});
	}
}

function add_new_tournament(){
/**	jQuery.post(BASE_URI+'admin/dataman/tournament/add_new_tournament',{},function(res){
		jQuery('#dialog').html(res);
		jQuery('#dialog').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:400,
				 title: 'Add tournament' 
			}
		);
	});
**/
	jQuery('#popupwindow').dialog(
			{
				 width: 950,
				 resizable: false,
				 height:600,
				 title: 'Add tournament' 
			}
		);
}

function tournament_edit(id, task){
/**	jQuery.post(BASE_URI+'admin/dataman/tournament/edit_tournament',{id:id},function(res){
		jQuery('#dialog').html(res);
		jQuery('#dialog').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:400,
				 title: 'Edit tournament' 
			}
		);
	});
**/
	queryURL(BASE_URI+'admin/dataman/tournament/'+task+'/?id='+id);	
}

function prize_add(tournament_id){
	jQuery.post(BASE_URI+'admin/dataman/tournament/add_prize',{tournament_id:tournament_id},function(res){
		jQuery('#dialog').html(res);
		jQuery('#dialog').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:600,
				 title: 'Add prize' 
			}
		);
	});
}

function prize_view(tournament_id){
	jQuery.post(BASE_URI+'admin/dataman/tournament/view_prize',{tournament_id:tournament_id},function(res){
		jQuery('#dialog').html(res);
		jQuery('#dialog').dialog(
			{
				 width: 900,
				 resizable: true,
				 height:500,
				 title: 'View prize' 
			}
		);
	});
}

function prize_edit(p_id){
	jQuery.post(BASE_URI+'admin/dataman/tournament/edit_prize',{p_id:p_id},function(res){
		jQuery('#dialog2').html(res);
		jQuery('#dialog2').dialog(
			{
				 width: 650,
				 resizable: false,
				 height:600,
				 title: 'Edit prize' 
			}
		);
	});
}

jQuery(function() {
	jQuery("#start_time_btn").live('click',function(){
		jQuery('.DynarchCalendar-topCont').css({top:100,width:60});
		Calendar.setup({
			inputField : "start_time",
			trigger    : "start_time_btn",
			onSelect   : function() { this.hide() },
			showTime   : 24,
			dateFormat : "%Y-%m-%d %I:%M"
		});
	});
	jQuery("#end_time_btn").live('click',function(){
		jQuery('.DynarchCalendar-topCont').css({top:100,width:60});
		Calendar.setup({
			inputField : "end_time",
			trigger    : "end_time_btn",
			onSelect   : function() { this.hide() },
			showTime   : 24,
			dateFormat : "%Y-%m-%d %I:%M"
		});
	});
	jQuery("#shipping_date_btn").live('click',function(){
		jQuery('.DynarchCalendar-topCont').css({top:100,width:60});
		Calendar.setup({
			inputField : "shipping_date",
			trigger    : "shipping_date_btn",
			onSelect   : function() { this.hide() },
			showTime   : 24,
			dateFormat : "%Y-%m-%d"
		});
	});
	
});

jQuery(document).ready(function() {
	var optionsTournament = { 
		beforeSubmit:beforeSubmitTournament,   
		success:afterSubmitTournament
	};	
	var optionsPrize = {
		beforeSubmit:beforeSubmitPrize,
		success:afterSubmitPrize
	};
		
	jQuery("#tournament_submit").live("submit",function(e){
		e.preventDefault(); // <-- important
		jQuery(this).ajaxSubmit(optionsTournament);
		return false;
	});	
	jQuery("#prize_submit").live("submit",function(e){
		e.preventDefault(); // <-- important
		jQuery(this).ajaxSubmit(optionsPrize);
		return false;
	});	
	
});


function beforeSubmitPrize(formData, jqForm, options){
	jQuery('#loader').show();
	
	json = {};
	for( e in formData){
		json[formData[e].name] = formData[e].value;
	}
	
	var arrayRequire = [ 'rank','prize']; 
	var err = 0;
	for(var i=0 ;i< arrayRequire.length; i++ ){
		if( isEmpty(json[arrayRequire[i]]) ){
			jQuery('#'+arrayRequire[i]+'_error').show();
			err++;
		}else{
			jQuery('#'+arrayRequire[i]+'_error').hide();
		}
	}
	
	if(err > 0){	
		jQuery('#loader').hide();
		alert("Error. Any fields are missing.");
		return false;
	}else{
		return true;
	}
} 
 
function afterSubmitPrize(responseText, statusText, xhr, $form){
	jQuery('#loader').hide();	
	 
	if(responseText == 'ok'){
		jQuery('#dialog').dialog('close');
		reload();
	}else{
		alert(responseText);
	}
} 

function afterSubmitTournament(responseText, statusText, xhr, $form) {
	jQuery('#loader').hide();	
	 
	if(responseText == 'ok'){
		jQuery('#dialog').dialog('close');
		reload();
	}else{
		alert(responseText);
	}
}

function beforeSubmitTournament(formData, jqForm, options){
	jQuery('#loader').show();
	
	json = {};
	for( e in formData){
		json[formData[e].name] = formData[e].value;
	}
	
	var arrayRequire = [ 'name','start_time','end_time','description']; 
	var err = 0;
	for(var i=0 ;i< arrayRequire.length; i++ ){
		if( isEmpty(json[arrayRequire[i]]) ){
			jQuery('#'+arrayRequire[i]+'_error').show();
			err++;
		}else{
			jQuery('#'+arrayRequire[i]+'_error').hide();
		}
	}
	
	if(err > 0){	
		jQuery('#loader').hide();
		alert("Error. Any fields are missing.");
		return false;
	}else{
		return true;
	}
}